//
//  AnswerDetailViewController.m
//  RainbowFM
//
//  Created by chenli on 14/12/28.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "AnswerDetailViewController.h"
#import "ActivityModel.h"


@interface AnswerDetailViewController (){
    NSInteger checkIndex;
    NSArray *questionArray;
    NSString *ansStr;
}
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;

@property (nonatomic,strong) PrizeDetailItem *detailItem;

@end

@implementation AnswerDetailViewController


-(void)fetchData{
    [[ActivityModel sharedModel] fetchPrizeDetailWithActID:_actId result:^(BOOL success, PrizeDetailItem *item) {
        if (success) {
            self.detailItem = item;
            [self initViewData];
        }
    }];
}


-(void)initViewData{
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:_detailItem.prizeUrl]];
    self.infoLabel.text = [NSString stringWithFormat:@"本题有效期至 %@ 日结束。答题结束后到“我的奖品”中查看是否中奖",_detailItem.endDt];
    self.desLabel.text = [NSString stringWithFormat:@"%@",_detailItem.qstContent];
    questionArray = [_detailItem.qstAnsOpt componentsSeparatedByString:@"∮"];
    [self.tableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    checkIndex = -1;
    [self fetchData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return questionArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    
    cell.textLabel.text = questionArray[indexPath.row];
    if (checkIndex == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        checkIndex = indexPath.row;
        ansStr = cell.textLabel.text;
    }else{
        checkIndex = -1;
    }
    
    [self.tableView reloadData];
}

- (IBAction)commitAction:(id)sender {
    if (checkIndex == -1) {
        [SVProgressHUD showErrorWithStatus:@"请选择答案"];
    }else{
//        [SVProgressHUD showSuccessWithStatus:@"恭喜您回答真确!"];
//        [self.navigationController popViewControllerAnimated:YES];
    }
    
    NSString *queStr = [NSString stringWithFormat:@"%@∮%@",_detailItem.qstId,ansStr];
    [[ActivityModel sharedModel] submitQuestionWithActID:_actId
                                              ansContent:queStr
                                                  result:^(BOOL success, NSString *msg) {
                                                      if (success) {
                                                          
                                                      }else{
                                                          [SVProgressHUD showErrorWithStatus:msg];
                                                      }
                                                  }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
